package com.acmedcare.framework.exchange.center.endpoint;

import com.acmedcare.framework.exchange.center.common.Page;
import com.acmedcare.framework.exchange.center.core.Const;
import com.acmedcare.framework.exchange.center.endpoint.exception.InvalidRequestParameterException;
import com.acmedcare.framework.exchange.center.endpoint.service.PluginService;
import com.acmedcare.framework.exchange.center.entity.bean.Plugin;
import com.acmedcare.framework.exchange.center.entity.bean.PluginStatusEnum;
import com.acmedcare.framework.exchange.center.entity.dto.PluginCommand;
import com.acmedcare.framework.exchange.center.entity.dto.PluginQueryDto;
import com.acmedcare.framework.starter.bus.AcmedcareEventPublisher;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/plugins")
@Api(tags = "插件管理")
public class PluginEndpoint {

  final PluginService pluginService;

  final RabbitTemplate rabbitTemplate;
  final ObjectMapper objectMapper;
  final AcmedcareEventPublisher publisher;

  public PluginEndpoint(
      PluginService pluginService,
      RabbitTemplate rabbitTemplate, ObjectMapper objectMapper,
      AcmedcareEventPublisher publisher) {
    this.pluginService = pluginService;
    this.rabbitTemplate = rabbitTemplate;
    this.objectMapper = objectMapper;
    this.publisher = publisher;
  }

  @GetMapping("/list")
  @ApiOperation("获取任务列表")
  public ResponseEntity<Page<Plugin>> getList(PluginQueryDto queryDto) {

    return ResponseEntity.ok(pluginService.getList(queryDto));
  }

  @PostMapping("")
  @ApiOperation("新增插件")
  public ResponseEntity add(@RequestBody Plugin plugin) throws InvalidRequestParameterException {

    validPlugin(plugin);
    plugin.setStatus(PluginStatusEnum.UNLOAD);
    pluginService.add(plugin);
    return ResponseEntity.noContent().build();
  }

  @PutMapping("/{pluginId}")
  @ApiOperation("删除插件")
  public ResponseEntity update(
      @PathVariable @ApiParam(value = "插件id", required = true) Long pluginId,
      @RequestBody Plugin plugin)
      throws InvalidRequestParameterException {

    validPlugin(plugin);
    plugin.setPluginId(pluginId);
    pluginService.update(plugin);
    return ResponseEntity.noContent().build();
  }

  @DeleteMapping("/{pluginId}")
  @ApiOperation("修改插件")
  public ResponseEntity delete(
      @PathVariable @ApiParam(value = "插件id", required = true) Long pluginId)
      throws JsonProcessingException {

    pluginService.delete(pluginId);
    command(pluginId, PluginStatusEnum.UNLOAD, "");
    return ResponseEntity.noContent().build();
  }

  @PostMapping("/{pluginId}/{status}")
  public ResponseEntity command(@PathVariable Long pluginId,
      @PathVariable PluginStatusEnum status,
      @ApiParam("指定分组环境") @RequestParam(value = "groupName", defaultValue = "") String groupName)
      throws JsonProcessingException {

    Plugin plugin = pluginService.get(pluginId);
    PluginCommand pluginCommand = new PluginCommand();
    pluginCommand.setPluginId(pluginId);
    pluginCommand.setStatusEnum(status);
    pluginCommand.setPlugin(plugin);
    pluginCommand.setGroupName(groupName);

    //amqpTemplate.se
    rabbitTemplate.setExchange(Const.PluginExchangeName);
    rabbitTemplate.convertAndSend(objectMapper.writeValueAsString(pluginCommand));

    return ResponseEntity.noContent().build();
  }

  private void validPlugin(Plugin plugin) throws InvalidRequestParameterException {

    if (StringUtils.isAnyEmpty(plugin.getName(), plugin.getBasePackageName(),
        plugin.getJarName(), plugin.getJarPath(), plugin.getVersion())) {

      throw new InvalidRequestParameterException("请确认所有非空字段都已经填写完成！");
    }
  }
}